package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.CheckGroup;
import com.itheima.VO.Checkgroup_ids;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.checkgroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {

    @Reference
    private checkgroupService checkgroupService;

    /**
     * 查询所有的检查项
     *
     * @return
     */
    ///checkgroup/findAllGroup.do
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findAllGroup")
    public Result findAllGroup() {
        Result result = checkgroupService.findAllGroup();
        return result;

    }


    /*
     /checkgroup/addCheckgroup

     */
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/addCheckgroup")
    public Result addCheckgroup(@RequestBody Checkgroup_ids checkgroup_ids) {
        //   System.out.println(checkgroup_ids);
        //{"formData":{"code":"1234123","name":"423134","helpCode":"1234","remark":"1234","attention":"1234","sex":"1"},"checkitemIds":[28,29,30]}
        try {
            checkgroupService.addCheckgroup(checkgroup_ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    ///checkgroup/findPage.do
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //前端传过来的json字符串,我们需要将它转为原来的样子
        PageResult pageResult = checkgroupService.findPage(queryPageBean);
        return pageResult;
    }

    ///checkgroup/findCheckgroupById.do?id=" + row.id
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/findCheckgroupById")
    public Result findCheckgroupById(Integer id) {
        CheckGroup checkGroup = checkgroupService.findCheckgroupById(id);
        if (checkGroup != null) {
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } else {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //根据检查组id去找对应的检查项
    //checkgroup/findCehckitemsByCheckgroupId.do?id="+row.id
    @RequestMapping("/findCehckitemsByCheckgroupId")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Integer[] findCehckitemsByCheckgroupId(Integer id) {
        Integer[] ids = checkgroupService.findCehckitemsByCheckgroupId(id);
        if (ids == null || ids.length == 0) {
            return null;
        } else {
            return ids;
        }
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Checkgroup_ids checkgroup_ids) {
        try {
            checkgroupService.edit(checkgroup_ids.getFormData(), checkgroup_ids.getCheckitemIds());
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        Result result = checkgroupService.delete(id);
        return result;
    }
}
